#ifndef OTA_H
#define OTA_H
#include <stdint.h>
#include <cmsis_armclang.h>

#define BOOTLOADER_IMG_RSV_SIZE          (240*1024)
#define OTA_B_STORAGE_ADDRESS            ((4+4)*1024 + BOOTLOADER_IMG_RSV_SIZE)
#define IMAGE_INFO_RSV_SIZE              0x2000   //8K
#define APP_CODE_INFO_OFFSET             0x148
#define IMGAE_INFO_SIZE                  0x10
#define OTA_BOOT_INFO_ADDRESS            (OTA_B_STORAGE_ADDRESS + BOOTLOADER_IMG_RSV_SIZE)
#define APP_CODE_STORAGE_ADDRESS         (OTA_BOOT_INFO_ADDRESS+IMAGE_INFO_RSV_SIZE)     
#define RETARGET_APP_CODE_ADDRESS        (FLASH_DAC_BASE+APP_CODE_STORAGE_ADDRESS) //APPµØÖ·
#define FILE_CODE_ADDRESS_OFFSET         IMGAE_INFO_SIZE   


#define FLASH_SIZE                       0x200000    // 2M
#define DSP_SIZE                         (256*1024)       // DSP SIZE 152k
#define CONTROLLER_SIZE                  (80*1024)

#define DSP_STORAGE_ADDRESS              (FLASH_SIZE-DSP_SIZE)           
#define CONTROLLER_STORAGE_ADDRESS       (DSP_STORAGE_ADDRESS-CONTROLLER_SIZE)


#define DSP_START_ADDRESS               (DSP_STORAGE_ADDRESS+FILE_CODE_ADDRESS_OFFSET)
#define CONTROLLER_START_ADDRESS        (CONTROLLER_STORAGE_ADDRESS+FILE_CODE_ADDRESS_OFFSET)

#define OTA_HDR_RESULT_LEN               1
#define OTA_HDR_OPCODE_LEN               1
#define OTA_HDR_LENGTH_LEN               2

typedef enum
{
    OTA_CMD_NVDS_TYPE,
    OTA_CMD_GET_STR_BASE,
    OTA_CMD_READ_FW_VER,    //read firmware version
    OTA_CMD_PAGE_ERASE,
    OTA_CMD_CHIP_ERASE,
    OTA_CMD_WRITE_DATA,
    OTA_CMD_READ_DATA,
    OTA_CMD_WRITE_MEM,
    OTA_CMD_READ_MEM,
    OTA_CMD_REBOOT,
    OTA_CMD_START,
    OTA_CMD_NULL,
} ota_cmd_t;

typedef enum
{
    FILE_BOOT_LOADER =1,
    FILE_APP,
    FILE_CONTROLLER,
    FILE_DSP,
}ota_file_type_t;

typedef enum 
{
    OTA_RSP_SUCCESS,
    OTA_RSP_ERROR,
    OTA_RSP_UNKNOWN_CMD,
}ota_rsp_t;
/*****************************************************************************
 * TYPEDEFS (类型定义)
 */
struct app_otas_status_t
{
    uint8_t  read_opcode;
    uint8_t  length;
    uint32_t base_addr;
};

__PACKED_STRUCT file_head_info_t{
    uint8_t  check[4];
    uint32_t version;
    uint32_t code_length;
    uint32_t crc;
};

void app_otas_recv_data(uint8_t conidx,uint8_t *p_data,uint16_t len);
void ota_check_file_init(void);
#endif
